<style lang="less" scoped>
.user-management {
  width: 100%;
  height: 100%;
  /deep/.el-table__inner-wrapper {
    height: 100% !important;
  }
  .pagination-css {
    padding: 5px;
    box-sizing: border-box;
    background-color: #fff;
    display: flex;
    justify-content: right;
    margin-top: 5px;
  }
}
</style>

<template>
  <div class="user-management">
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline form-css"
    >
      <el-form-item label="用户姓名：">
        <el-input v-model="formInline.userName" placeholder="请输入用户姓名" />
      </el-form-item>
      <el-form-item label="用户账号：">
        <el-input
          v-model="formInline.userAccount"
          placeholder="请输入用户账号"
        />
      </el-form-item>
      <el-form-item label="所属机构：">
        <el-select v-model="formInline.affiHospital" filterable placeholder="">
          <el-option v-for="(item, index) in projectList" :key="index" :label="item.insname" :value="item.insname" />
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="search"
          ><el-icon style="margin-right: 6px"><Search /></el-icon>
          搜索</el-button
        >
        <el-button @click="reset"
          ><el-icon style="margin-right: 6px"><Refresh /></el-icon>
          重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="filter-column">
      <el-button  @click="addUser"
        ><el-icon><Plus /></el-icon>新增用户</el-button
      >
    </div>
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      :header-cell-style="{ backgroundColor: '#EEF6FF' }"
      stripe
      border
      height="calc(100% - 218px)"
      style="width: 100%; padding: 10px"
      >
      <el-table-column label="用户姓名" align="center" prop="rname" />
      <el-table-column label="用户账号" align="center" prop="uname" />
      <el-table-column label="所属机构" align="center" prop="insname" />
      <el-table-column label="分配角色" align="center" prop="role">
        <template #default="{ row }">
          <span>{{
            row.role == "" || row.role == 0
              ? "普通医生"
              : row.role == 1
              ? "管理员"
              : row.role == 10
              ? "审核管理员"
              : row.role == 12
              ? "设备厂家"
              : row.role == 13
              ? "项目点（慧医）"
              : ""
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="tel" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="setPwd(row)"
            >重置密码</el-button
          >
          <el-button type="primary" link @click="editUser(row)">编辑</el-button>
          <el-button type="danger" link @click="deletUser(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageList.current"
      v-model:page-size="pageList.size"
      :page-sizes="[20, 40, 60, 80]"
      small="small"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageList.total"
      class="pagination-css"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      v-model="userVisible"
      width="670px"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <template #title>
        <span>{{ userTitle }}</span>
      </template>
      <el-form :model="userForm" inline>
        <el-form-item label="用户姓名：">
          <el-input
            v-model="userForm.rname"
            placeholder="请输入用户姓名"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="联系方式：">
          <el-input
            v-model="userForm.tel"
            placeholder="请输入联系方式"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="用户账号：">
          <el-input
            v-model="userForm.uname"
            placeholder="请输入用户账号"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item v-if="userTitle == '新增用户'" label="初始密码：">
          <el-input
            v-model="userForm.psd"
            type="password"
            show-password
            placeholder="请输入当前密码"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="所属机构：">
          <el-cascader
            ref="hospital"
            :props="userProps"
            filterable
            @change="handleNodeClick"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item v-if="userTitle == '编辑用户'" label="当前机构：">
          <span style="display: inline-block; width: 200px">{{
            userForm.hospital
          }}</span>
        </el-form-item>
        <el-form-item label="用户角色：">
          <el-select
            v-model="userForm.role"
            placeholder="请选择用户角色"
            style="width: 200px"
          >
            <el-option
              v-for="(item, index) in roleList"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="菜单：" style="width: 100%">
          <el-tree
            ref="treeMenu"
            :props="treeProps"
            :data="treeData"
            show-checkbox
            node-key="id"
            style="
              min-height: 140px;
              max-height: 390px;
              overflow: auto;
              width: 100%;
            "
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="" @click="userVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
    <el-dialog
      v-model="setPwdVisible"
      width="470px"
      title="重置密码"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-form :model="userForm" inline>
        <el-form-item label="密码：">
          <el-input
            v-model="newPwd"
            type="password"
            placeholder="请输入密码"
            style="width: 200px"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="" @click="setPwdVisible = false">取消</el-button>
        <el-button type="primary" @click="submitPwd">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getSubIns, getAllSubIns } from "@/apis/hospitalManagement";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  getUserLoginList,
  AddUserLoginWeb,
  deleteUserLogin,
} from "@/apis/userManagement";
import md5 from "js-md5";
import { adminmodify } from "@/apis/user";
export default {
  data() {
    let _this = this;
    return {
      projectList: [],
      setPwdVisible: false,
      formInline: {
        userName: "",
        userAccount: "",
        affiHospital: "",
      },
      userTitle: "新增用户",
      hosProps: {
        lazy: true,
        checkStrictly: true,
        label: "insname",
        value: "insname",
        lazyLoad(node, resolve) {
          _this.getJunior(node, resolve);
        },
      },
      userProps: {
        lazy: true,
        checkStrictly: true,
        label: "insname",
        value: "inscode",
        lazyLoad(node, resolve) {
          _this.getUserHos(node, resolve);
        },
      },
      tableLoading: false,
      tableData: [],
      pageList: {
        current: 1,
        size: 20,
        total: 0,
      },
      userVisible: false,
      userForm: {
        rname: "",
        tel: "",
        uname: "",
        psd: "",
        hospital: "",
        hospitalId: "",
        role: "",
      },
      hospitalList: [],
      roleList: [
        {
          label: "普通医生",
          value: "0",
        },
        {
          label: "管理员",
          value: "1",
        },
        {
          label: "审核管理员",
          value: "10",
        },
        {
          label: "设备厂家",
          value: "12",
        },
        {
          label: "项目点（慧医）",
          value: "13",
        },
        {
          label: "项目点（国药）",
          value: "14",
        },
      ],
      newPwd: "",
      setPwdInfo: {},
      treeProps: {
        label: "label",
        children: "childrens",
      },
      treeData: [
        {
          label: "报告列表",
          path: "/reportList",
          name: "reportList",
          icon: "Document",
          parentId: "0",
          id: "1",
          childrens: [
            {
              path: "/reportList/echocardiography",
              name: "/echocardiography",
              label: "颈动脉项目",
              id: "6",
              parentId: "1",
              childrens: [
                {
                  path: "/reportList/echocardiography/echCardiovascularProgramInside",
                  name: "/echCardiovascularProgramInside",
                  label: "颈动脉心血管项目(内部管理)",
                  id: "17",
                  parentId: "6",
                },
                {
                  path: "/reportList/echocardiography/echCardiovascularProgramPoint",
                  name: "/echCardiovascularProgramPoint",
                  label: "颈动脉心血管项目",
                  id: "18",
                  parentId: "6",
                },
                {
                  path: "/reportList/echocardiography/echCerebrovascularProjectInside",
                  name: "/echCerebrovascularProjectInside",
                  label: "颈动脉脑卒中项目(内部管理)",
                  id: "19",
                  parentId: "6",
                },
                {
                  path: "/reportList/echocardiography/echCerebrovascularProjectPoint",
                  name: "/echCerebrovascularProjectPoint",
                  label: "颈动脉脑卒中项目",
                  id: "20",
                  parentId: "6",
                },
              ],
            },
            {
              path: "/reportList/otherProject",
              name: "/otherProject",
              label: "颈动脉非国家项目",
              id: "7",
              parentId: "1",
              childrens: [
                {
                  path: "/reportList/otherProject/hncyPoint",
                  name: "/hncyPoint",
                  label: "河南村医项目",
                  id: "21",
                  parentId: "7",
                },
                {
                  path: "/reportList/otherProject/hyPoint",
                  name: "/hyPoint",
                  label: "慧医项目",
                  id: "22",
                  parentId: "7",
                },
                {
                  path: "/reportList/otherProject/gjPoint",
                  name: "/gjPoint",
                  label: "筛查项目",
                  id: "23",
                  parentId: "7",
                },
                {
                  path: "/reportList/otherProject/gyPoint",
                  name: "/gyPoint",
                  label: "国药爱心",
                  id: "24",
                  parentId: "7",
                },
                {
                  path: "/reportList/otherProject/xjbctPoint",
                  name: "/xjbctPoint",
                  label: "新疆百草堂",
                  id: "29",
                  parentId: "7",
                },
                 {
                  path: "/reportList/otherProject/xxgyjyPoint",
                  name: "/xxgyjyPoint",
                  label: "心血管研究院",
                  id: "30",
                  parentId: "7",
                },
              ],
            },
          ],
        },
        {
          path: "/reportReview",
          name: "/reportReview",
          label: "报告审核",
          icon: "DocumentCopy",
          parentId: "0",
          id: "2",
          childrens: [
            {
              path: "/reportReview/carotidArtery",
              name: "/reportReview/carotidArtery",
              label: "超声心动图",
              id: "8",
              parentId: "2",
            },
            {
              path: "/reportReview/echocardiography",
              name: "/reportReview/echocardiography",
              label: "颈动脉",
              id: "9",
              parentId: "2",
            },
          ],
        },
        // {
        //   path: "/reportStatistics",
        //   name: "reportStatistics",
        //   label: "报告统计",
        //   icon: "DataAnalysis",
        //   parentId: "0",
        //   id: "3",
        //   childrens: [
        //     {
        //       path: "/reportStatistics/reportAudit",
        //       name: "reportAudit",
        //       label: "审核类",
        //       id: "10",
        //       parentId: "3",
        //     },
        //     {
        //       path: "/reportStatistics/reportNonAudit",
        //       name: "reportNonAudit",
        //       label: "非审核类",
        //       id: "11",
        //       parentId: "3",
        //     },
        //   ],
        // },
           {
          path: "/statisticalAnalysis",
          name: "statisticalAnalysis",
          label: "统计分析",
          icon: require("@/images/menu3.png"),
          parentId: "0",
          id: "3",
          childrens: [
            {
              path: "/statisticalAnalysis/screeningSituation",
              name: "screeningSituation",
              label: "筛查情况",
              parentId: "3",
              id: "31",
            },
            {
              path: "/statisticalAnalysis/operationSituation",
              name: "operationSituation",
              label: "运营情况看板",
              parentId: "3",
              id: "32",
            },
            {
              path: "/statisticalAnalysis/reportStatistics",
              name: "reportStatistics",
              label: "报告统计",
              parentId: "3",
              id: "33",
              childrens: [
                {
                  path: "/statisticalAnalysis/reportStatistics/reportAudit",
                  name: "reportAudit",
                  label: "审核类",
                  id: "10",
                  parentId: "33",
                },
                {
                  path: "/statisticalAnalysis/reportStatistics/reportNonAudit",
                  name: "reportNonAudit",
                  label: "非审核类",
                  id: "11",
                  parentId: "33",
                },
              ],
            },
          ],
        },
        {
          path: "/sessionInfo",
          name: "sessionInfo",
          label: "场次信息",
          icon: "Finished",
          parentId: "0",
          id: "4",
          childrens: [
            {
              path: "/sessionInfo/huiyiProject",
              name: "huiyiProject",
              label: "场次-慧医",
              id: "12",
              parentId: "4",
            },
          ],
        },
        {
          path: "/systemManagement",
          name: "systemManagement",
          label: "系统管理",
          icon: "Setting",
          parentId: "0",
          id: "5",
          childrens: [
            {
              path: "/systemManagement/equipmentManagement",
              name: "equipmentManagement",
              label: "设备管理",
              id: "13",
              parentId: "5",
            },
            {
              path: "/systemManagement/equipmentBind",
              name: "equipmentBind",
              label: "设备绑定",
              id: "34",
              parentId: "5",
            },
            {
              path: "/systemManagement/pactManagement",
              name: "pactManagement",
              label: "合约管理",
              id: "14",
              parentId: "5",
              childrens: [
                {
                  path: "/systemManagement/equipmentManagement/pactAudit",
                  name: "pactAudit",
                  label: "审核类",
                  id: "25",
                  parentId: "14",
                },
                {
                  path: "/systemManagement/equipmentManagement/pactNonAudit",
                  name: "pactNonAudit",
                  label: "非审核类",
                  id: "26",
                  parentId: "14",
                },
              ],
            },
            {
              path: "/systemManagement/hospitalManagement",
              name: "hospitalManagement",
              label: "机构管理",
              id: "15",
              parentId: "5",
              childrens: [
                {
                  path: "/systemManagement/hospitalManagement/hospitalManagementInside",
                  name: "hospitalManagementInside",
                  label: "项目点管理",
                  id: "27",
                  parentId: "15",
                },
                {
                  path: "/systemManagement/hospitalManagement/auditHospitalManagement",
                  name: "auditHospitalManagement",
                  label: "审核机构管理",
                  id: "28",
                  parentId: "15",
                },
              ],
            },
            {
              path: "/systemManagement/userManagement",
              name: "userManagement",
              label: "用户管理",
              id: "16",
              parentId: "5",
            },
          ],
        },
      ],
    };
  },
  mounted() {
    this.getJunior()
    this.init();
  },
  methods: {
    init() {
      this.pageList.current = 1;
      this.getTableData();
    },
    getTableData() {
      this.tableData = [];
      this.tableLoading = true;
      getUserLoginList({
        page: this.pageList.current,
        size: this.pageList.size,
        uname: this.$store.getters.name,
        inscode: this.$store.getters.inscode,
        insname: this.formInline.affiHospital,
        username: this.formInline.userName,
        useraccount: this.formInline.userAccount,
      }).then((res) => {
        if (res.code == 0) {
          if (res.data && res.data.length > 0) {
            this.tableData = res.data;
          }
        }
        this.tableLoading = false;
        this.pageList.total = res.size;
      });
    },
    getJunior() {
      this.projectList = []
      getAllSubIns({
        uname: this.$store.getters.name,
        inscode: this.$store.getters.inscode,
        subins: '',
      }).then((res) => {
        if (res.code == 0) {
          if (res.data && res.data.length > 0) {
            this.projectList = res.data
          }
        }
      });
    },
    addUser() {
      this.userVisible = true;
      this.userTitle = "新增用户";
      this.userForm.rname = "";
      this.userForm.tel = "";
      this.userForm.uname = "";
      this.userForm.psd = "";
      this.userForm.hospital = "";
      this.userForm.hospitalId = "";
      this.userForm.role = "";
      this.$nextTick(() => {
        if (this.$refs.treeMenu) {
          this.$refs.treeMenu.setCheckedKeys([]);
        }
      });
    },
    getUserHos(node, resolve) {
      let subins = "";
      if (node.value) {
        subins = node.value;
      }
      // else {
      //   subins = this.$store.getters.inscode
      // }
      getSubIns({
        uname: this.$store.getters.name,
        inscode: this.$store.getters.inscode,
        subins: subins,
      }).then((res) => {
        if (res.code == 0) {
          if (res.data && res.data.length > 0) {
            resolve(res.data);
          } else {
            resolve([]);
          }
        } else {
          resolve([]);
        }
      });
    },
    search() {
      this.getTableData();
    },
    reset() {
      this.formInline.affiHospital = [];
      this.formInline.userName = "";
      this.formInline.userAccount = "";
      this.getTableData();
    },
    handleNodeClick(data) {
      console.log(data, "data");
    },
    setPwd(row) {
      this.newPwd = "";
      this.setPwdVisible = true;
      this.setPwdInfo = row;
    },
    submitPwd() {
      let md5Psd = "";
      if (this.newPwd) {
        md5Psd = md5(this.newPwd);
      }
      adminmodify({
        uname: this.setPwdInfo.uname,
        upwd: md5Psd,
        rname: "",
        insname: "",
        inscode: "",
        checker: "",
      }).then((res) => {
        console.log(res, "res");
        if (res.data.status == 0) {
          ElMessage({
            type: "success",
            message: "重置成功",
          });
          this.getTableData();
          this.setPwdVisible = false;
        }
      });
    },
    editUser(row) {
      console.log(row, "row");
      this.userTitle = "编辑用户";
      let rowData = JSON.parse(JSON.stringify(row));
      this.userForm = rowData;
      if (row.role == "" || row.role == 0) {
        this.userForm.role = "0";
      }
      this.userForm.hospital = row.insname;
      this.userForm.hospitalId = row.inscode;
      this.userVisible = true;
      let menuArr = [];
      if (row.rolemenu) {
        menuArr = row.rolemenu.split(",");
      }
      this.$nextTick(() => {
        if (this.$refs.treeMenu) {
          this.$refs.treeMenu.setCheckedKeys(menuArr);
        }
      });
    },
    deletUser(row) {
      ElMessageBox.confirm("确定删除该用户？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteUserLogin({
            uname: this.$store.getters.name,
            inscode: this.$store.getters.inscode,
            deluname: row.uname,
          }).then((res) => {
            if (res.code == 0) {
              ElMessage({
                type: "success",
                message: "删除成功",
              });
              this.getTableData();
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消",
          });
        });
    },
    dealMenu(treeData, HalfTreeData) {
      let data = [];
      let res = [];
      let map = {};
      data = treeData.concat(HalfTreeData);
      for (let v of data) {
        v.children = [];
        map[v.id] = v;
      }
      for (let v of data) {
        let parent = map[v.parentId];
        if (parent) {
          if (!parent.children) parent.children = [];
          parent.children.push(v);
        } else {
          res.push(v);
        }
      }
      return res;
    },
    submit() {
      // let HalfTreeData = []
      let treeId = [];
      let treeData = this.$refs.treeMenu.getCheckedNodes();
      console.log(treeData, "treeData");
      if (treeData && treeData.length > 0) {
        treeData.forEach((el) => {
          treeId.push(el.id);
        });
      }
      console.log(treeId, "treeId");
      // return
      // HalfTreeData = this.$refs.treeMenu.getHalfCheckedNodes()
      // let roleMenu = this.dealMenu(treeData, HalfTreeData)
      let hosInfo = this.$refs.hospital.getCheckedNodes();
      let psd = "";
      if (this.userTitle == "新增用户") {
        psd = md5(this.userForm.psd);
      } else if (this.userTitle == "编辑用户") {
        psd = this.userForm.upwd;
      }
      let inscode =
        hosInfo.length > 0 ? hosInfo[0].value : this.userForm.hospitalId;
      let insname =
        hosInfo.length > 0 ? hosInfo[0].label : this.userForm.hospital;
      let data = {
        addr: "",
        checker: "",
        ctime: "",
        inscode: inscode,
        insname: insname,
        insinfo: "",
        insl1: "",
        insl2: "",
        insl3: "",
        insl4: "",
        insl5: "",
        insl6: "",
        instel: "",
        ipflt: "",
        ltime: "",
        openid: "",
        patient: "",
        pri: 0,
        record_ref: "",
        rname: this.userForm.rname,
        role: this.userForm.role,
        sex: "",
        sn: "",
        stm32id: "",
        tel: this.userForm.tel,
        uname: this.userForm.uname,
        upwd: psd,
        rolemenu: treeId && treeId.length > 0 ? treeId.join(",") : "",
      };
      AddUserLoginWeb({
        uname: this.$store.getters.name,
        inscode: this.$store.getters.inscode,
        data: JSON.stringify(data),
      }).then((res) => {
        if (res.code == 0) {
          ElMessage({
            message: "新增成功",
            type: "success",
          });
          this.userVisible = false;
          this.init();
        }
      });
    },
    handleSizeChange(val) {
      this.pageList.current = 1;
      this.pageList.size = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.pageList.current = val;
      this.getTableData();
    },
  },
};
</script>

